1 #ifndef SINLIST_H_INCLUDED
2 #define SINLIST_H_INCLUDED
3
4 #include<iostream>
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<
string.h>
8 #include<conio.h>

9 using
namespace std;
10
11 class
LinkedList
12 {
13     
private:
14         
struct saver
15         {
16             
char pname[30]; //Product name
17             
int pid; //Product id
18             
int price; //Product price
19             
int tquant; //Total products purchased from wholesale
20             
int aquant; //How many are available in stock
21             
int rack; //Rack number where product is stored
22         };
23         
struct saver s;
24         
struct node
25         {
26             
char pname[30]; //Product name
27             
int pid; //Product id
28             
int price; //Product price
29             
int tquant; //Total products purchased from wholesale
30             
int aquant; //How many are available in stock
31             
int rack; //Rack number where product is stored
32             
struct node *link;
33         };
34
35         typedef
struct node *nodeptr; //nodeptr is a user-defined
36                                         
//data-type of type struct node *
37
38         nodeptr H;
39
40     
public:
41         LinkedList();
//Constructor
42         ~LinkedList();
//Destructor
43         
struct node * get_node() //Create a new node
44         {
45                 
return (new node);
46         }
47         
int getcount();
48         
void add_front(char [] , int , int , int , int , int );
49         
void add_end(char [], int , int , int , int , int );
50         
void add_middle(int,char [], int , int , int , int , int );
51         
void delete_node(int);
52         
void delete_node(char []);
53         
void display();
54         
void Search();
55         
void modify();
56         
void billgen(char [], char [], int, int &);
57         
void SaveToFile();
58         
void ReadFromFile(LinkedList &);
59         
int recsnum()
60         {
61             FILE *fp;
62             fp = fopen(
"product_store.txt","r");
63             fseek(fp,
0,2);
64             
int recs = ftell(fp)/sizeof(s);
65             fclose(fp);
66             
return recs;
67         }
68 };
69
70 LinkedList::LinkedList()
71 {
72     H = NULL;
73 }
74 LinkedList::~LinkedList()
75 {
76     nodeptr T;
77     
while(H != NULL)
78     {
79         T = H;
80         H = T->link;
81         
//cout << "Deleting node with data = " << T->pname << endl;
82         delete T;
83     }
84 }

85 int
LinkedList::getcount()
86 {
87     nodeptr T;
88     T = H;
89     
int c = 0;
90     
while(T != NULL)
91     {
92         T = T->link;
93         c++;
94     }
95     
return c;
96 }

97 void
LinkedList::add_front(char pname[], int pid, int price, int tquant, int aquant, int rack)
98 {
99     
if(H == NULL)
100     {
101         H = get_node();
102
103         strcpy(H->pname,pname);
104         H->pid = pid;
105         H->price = price;
106         H->tquant = tquant;
107         H->aquant = aquant;
108         H->rack = rack;
109
110         H->link = NULL;
111     }
112     
else
113     {
114         nodeptr N;
115         N = get_node();
116
117         strcpy(N->pname,pname);
118         N->pid = pid;
119         N->price = price;
120         N->tquant = tquant;
121         N->aquant = aquant;
122         N->rack = rack;
123
124         N->link = H;
125         H = N;
126     }
127
128 }

129 void
LinkedList::add_end(char pname[], int pid, int price, int tquant, int aquant, int rack)
130 {
131     
if(H == NULL)
132     {
133         H = get_node();
134
135         strcpy(H->pname,pname);
136         H->pid = pid;
137         H->price = price;
138         H->tquant = tquant;
139         H->aquant = aquant;
140         H->rack = rack;
141
142         H->link = NULL;
143     }
144     
else
145     {
146         nodeptr T;
147         T = H;
148         
while(T->link != NULL) //Traverse to last node
149             T = T->link;
150         T->link = get_node();
151
152         strcpy(T->link->pname,pname);
153         T->link->pid = pid;
154         T->link->price = price;
155         T->link->tquant = tquant;
156         T->link->aquant = aquant;
157         T->link->rack = rack;
158
159         T->link->link = NULL;
160     }
161 }

162 void
LinkedList::add_middle(int pos, char pname[], int pid, int price, int tquant, int aquant, int rack)
163 {
164     nodeptr T,N;
165     T = H;
166     
if(pos == 1)
167         add_front(pname,pid,price,tquant,aquant,rack);
168     
else if(pos > getcount())
169         cout <<
"Enter a valid position" << endl;
170     
else
171     {
172         
for(int i = 1 ; i < pos - 1 ; i++)
173         {
174             T = T->link;
175         }
176         N = get_node();
177
178         strcpy(N->pname,pname);
179         N->pid = pid;
180         N->price = price;
181         N->tquant = tquant;
182         N->aquant = aquant;
183         N->rack = rack;
184
185         N->link = T->link;
186         T->link = N;
187     }
188 }

189 void
LinkedList::delete_node(int nodenum)
190 {
191     nodeptr T,P;
192     T = H;
193     
if(T == NULL);
194     
else if(T->link == NULL) //Only one node
195     {
196         H = NULL;
197         delete T;
198     }
199     
else
200     {
201         
if(nodenum > getcount())
202             cout <<
"Enter a valid index" << endl;
203         
else
204         {
205             
for(int i = 1 ; i < nodenum - 1 ; i++)
206             {
207                 T = T->link;
208             }
209             P = T->link;
210             T->link = P->link;
211             delete P;
212         }
213     }
214 }

215 void
LinkedList::delete_node(char name[])
216 {
217     nodeptr T,P;
218     T = H;
219     P = T;
220     
if(T == NULL);
221     
else if(stricmp(T->pname,name) == 0)
222     {
223         T = T ->link;
224         H = T;
225         delete P;
226         P = T;
227     }
228     
else
229     {
230         
int flag = 0;
231         
for(int i = 1 ; i < getcount() ; i++)
232         {
233             
if(stricmp(T->link->pname,name) == 0)
234             {
235                 flag =
1;
236                 
break;
237             }
238             T = T->link;
239         }
240         
if(flag == 1)
241         {
242             P = T->link;
243             T->link = T->link->link;
244             delete P;
245             cout <<
"Record deleted" << endl;
246         }
247         
else
248             cout <<
"Product not found" << endl;
249     }
250 }

251 void
LinkedList::display()
252 {
253     system(
"CLS");
254     nodeptr T;
255     
int cnt = 1;
256     T = H;
257     cout <<
"\n\nNumber of records = " << getcount() << endl;
258     cout <<
"\n\n";
259     cout << setw(
30) << "Product name" << "\t"
260                                 << 
"pid" << "\t"
261                                 << 
"Price" << "\t"
262                                 << 
"Tquant" << "\t"
263                                 << 
"Aquant" << "\t"
264                                 << 
"Rack" << "\t"
265                                 << 
"Address" << "\t\t"
266                                 << 
"Link" << endl << endl;
267     
if(T == NULL)
268         cout <<
"List empty" << endl;
269     
while(T != NULL)
270     {
271         cout << setw(
30) << T->pname << "\t"
272              << T->pid <<
"\t"
273              << T->price <<
"\t"
274              << T->tquant <<
"\t"
275              << T->aquant <<
"\t"
276              << T->rack <<
"\t"
277              << T <<
"\t" << T->link << endl;
278
279         cnt++;
280         T = T->link;
281     }
282     cout<<
"\n\n";
283     system(
"PAUSE");
284 }

285
286 void
LinkedList::modify()
287 {
288     system(
"CLS");
289     nodeptr T;
290     
char pname[30];
291     
//int data;
292     cout <<
"Enter name of product whose record has to be modified : ";
293     cin >> pname;
294     
int cnt = 1;
295     
int flag = 0;
296     T = H;
297     
if(T == NULL)
298         cout <<
"List empty" << endl;
299     
while(T != NULL)
300     {
301         
if(stricmp(T->pname,pname) == 0)
302         {
303             cout <<
"----------------------------------------------------------------" << endl;
304             cout <<
"Node number " << cnt << endl;
305             cout <<
"----------------------------------------------------------------" << endl;
306             cout <<
"Node address = " << T << endl;
307
308             cout <<
"Product name : " << T->pname << endl
309             << 
"Product id : " << T->pid << endl
310             << 
"Price : " << T->price << endl
311             << 
"Total products initially : " << T->tquant << endl
312             << 
"Available products : " << T->aquant << endl
313             << 
"Rack number : " << T->rack << endl << endl;
314
315             cout <<
"Re-enter data : ";
316             cin >> T->pname >> T->pid >> T->price >> T->tquant >> T->aquant >> T->rack;
317
318             flag =
1;
319             
break;
320         }
321         
if(flag == 1)
322             
break;
323         cnt++;
324         T = T->link;
325
326     }
327 }

328 void
LinkedList::SaveToFile()
329 {
330     FILE *fp;
331     fp = fopen(
"product_store.txt","w");
332     fclose(fp);
333     nodeptr T;
334     T = H;
335
336     
while(T != NULL)
337     {
338         strcpy(s.pname,T->pname);
339         s.pid = T->pid;
340         s.price = T->price;
341         s.tquant = T->tquant;
342         s.aquant = T->aquant;
343         s.rack = T->rack;
344
345         T = T->link;
346         fp = fopen(
"product_store.txt","a");
347         fwrite(&s,
sizeof(s),1,fp);
348         fclose(fp);
349     }
350 }

351
352 void
LinkedList::ReadFromFile(LinkedList &arg)
353 {
354     FILE *fp;
355     
int recs = recsnum();
356     fp = fopen(
"product_store.txt","r");
357     
for(int i = 1 ; i <= recs ; i++)
358     {
359         fread(&s,
sizeof(s),1,fp);
360         arg.add_end(s.pname,s.pid,s.price,s.tquant,s.aquant,s.rack);
361     }
362     fclose(fp);
363 }

364
365 void
LinkedList::billgen(char filename[],char name[], int quantity, int &total)
366 {
367     FILE *fp;
368     fp = fopen(filename,
"a");
369     
int flag = 0;
370     nodeptr T;
371     T = H;
372     
while(T != NULL)
373     {
374         
if(stricmp(T->pname,name) == 0)
375         {
376             
if(quantity > T->aquant)
377                 cout <<
"Requested number of products are not available" << endl;
378             
else
379             {
380                 fprintf(fp,
"%-30s\t%-6d\t%-6d\t\t%-6d\n",T->pname,quantity,T->price,quantity*T->price);
381                 T->aquant = T->aquant - quantity;
382                 total = total + quantity*T->price;
383                 flag =
1;
384                 
break;
385                 fclose(fp);
386             }
387         }
388         T = T->link;
389         
if(flag == 1)
390             
break;
391     }
392     
if(flag == 0)
393     {
394         fclose(fp);
395         cout <<
"Requested Product is not available" << endl;
396     }
397 }

398
399 void
LinkedList::Search()
400 {
401     system(
"CLS");
402     nodeptr T;
403     
char pname[30];
404
405     cout <<
"\n\nEnter product name to be searched : ";
406     cin >> pname;
407
408     
int flag = 0;
409     T = H;
410     
if(T == NULL)
411         cout <<
"List empty" << endl;
412     
while(T != NULL)
413     {
414         
if(stricmp(T->pname,pname) == 0)
415         {
416             cout <<
"----------------------------------------------------------------" << endl;
417
418             cout <<
"Product name : " << T->pname << endl
419             << 
"Product id : " << T->pid << endl
420             << 
"Price : " << T->price << endl
421             << 
"Total products initially : " << T->tquant << endl
422             << 
"Available products : " << T->aquant << endl
423             << 
"Rack number : " << T->rack << endl << endl;
424
425             cout <<
"----------------------------------------------------------------" << endl;
426             getch();
427             flag =
1;
428             
break;
429         }
430         
if(flag == 1)
431             
break;
432
433         T = T->link;
434
435     }
436     
if(flag == 0)
437     {
438         cout <<
"Product not found" << endl;
439         getch();
440     }
441 }
442
443 #endif
// SINLIST_H_INCLUDED


Gõ tìm kiếm nhanh...